/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package system;

import ejb.system.Utente;
import ejb.system.UtenteFacadeRemote;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author ciummo
 */
public class UserAction extends DispatchAction {

    /*
     * forward name="success" path=""
     */
    private final static String SUCCESS = "success";
    private final static String TONEWUSER = "toNewUser";
    private final static String SUCCESSSUBMITUSER = "successSubmitUser";
    private final static String TOUSERSLIST = "toUsersList";
    private final static String TOUSERDETAILS = "toUserDetails";
    private final static String TOUSEREDIT = "toUserEdit";
    
    
    
    //TODO : Merita mettere i ruoli nel database!

    private Utilities utility = new Utilities();

    public ActionForward userDetails(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
  
        //UserActionForm userForm = (UserActionForm) form;
        
        UserActionForm userForm = new UserActionForm();
        
        String method = request.getParameter("func");
        
        //request.getSession().setAttribute("UserActionForm", user);
        
        String username;
                
        try{
               username = request.getParameter("username");
        }catch(NumberFormatException nfe){
            
            username = request.getSession().getAttribute("idUser").toString();
        }

        UtenteFacadeRemote utenteFacade = (UtenteFacadeRemote) utility.callRemoteBean("UtenteFacade/remote");
        
        Utente utente = utenteFacade.find(username);
        
        UtenteBeanToUserForm utenteToUser = new UtenteBeanToUserForm();
        userForm = utenteToUser.convert(utente);
        
        request.getSession().setAttribute("UserActionForm", userForm);
        
        if (method.equalsIgnoreCase("edit")){
        
            return mapping.findForward(TOUSEREDIT);
        }
        else 
            return mapping.findForward(TOUSERDETAILS);
    }
    
    public ActionForward eraseUser(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        String username;
                
        try{
               username = request.getParameter("username");
        }catch(NumberFormatException nfe){
            
            username = request.getSession().getAttribute("idUser").toString();
        }
        
        
        
        UtenteFacadeRemote utenteFacade = (UtenteFacadeRemote) utility.callRemoteBean("UtenteFacade/remote");
        
        Utente utente = utenteFacade.find(username);
        
        utenteFacade.remove(utente);
        
        //UserActionForm user = new UserActionForm();
        //request.getSession().setAttribute("UserActionForm", user);
        return mapping.findForward(SUCCESSSUBMITUSER);
    }    
    
    public ActionForward newUser(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
  
        
        UserActionForm userForm = (UserActionForm) form;
        
        //UserActionForm user = new UserActionForm();
        //request.getSession().setAttribute("UserActionForm", user);
        return mapping.findForward(TONEWUSER);
    }
    
    public ActionForward submitUser(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        
        UserActionForm userForm = (UserActionForm) form;
        
        String method = request.getParameter("func");
        
        UtenteFacadeRemote utenteFacade = (UtenteFacadeRemote) utility.callRemoteBean("UtenteFacade/remote");
        
        UserFormToUtenteBean userToUtente = new UserFormToUtenteBean();
        
        
        if (method.equalsIgnoreCase("new")){
        
            utenteFacade.create(userToUtente.convert(userForm));
            //return mapping.findForward(SUCCESSSUBMITUSER);
        }
        
        else if (method.equalsIgnoreCase("edit")){
        
            utenteFacade.edit(userToUtente.convert(userForm));
            //return mapping.findForward(SUCCESSSUBMITUSER);
        }        
        
        return mapping.findForward(SUCCESSSUBMITUSER);
    }

    public ActionForward listUsers(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {    
        
        
        
        UtenteFacadeRemote utenteFacade = (UtenteFacadeRemote) utility.callRemoteBean("UtenteFacade/remote");
        UserActionForm userForm = (UserActionForm) form;        
        userForm.setList(utenteFacade.findAll());
        
        return mapping.findForward(TOUSERSLIST);
    }
    
    public ActionForward switchState(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {    
        
        String username;
                
        try{
               username = request.getParameter("username");
        }catch(NumberFormatException nfe){
            
            username = request.getSession().getAttribute("idUser").toString();
        }
        
        
        
        UtenteFacadeRemote utenteFacade = (UtenteFacadeRemote) utility.callRemoteBean("UtenteFacade/remote");
        
        Utente utente = utenteFacade.find(username);
        
        if (utente.getStato() == 0){
            
            utente.setStato(1);
        }
        else {
        
            utente.setStato(0);
        }
        
        utenteFacade.edit(utente);

        return mapping.findForward(SUCCESSSUBMITUSER);
    }    
    
}



class UserFormToUtenteBean{
    
    Utente convert (UserActionForm userForm){
        
        Utente utente = new Utente();
        
        
        utente.setUsername(userForm.getUsername());
        utente.setPassword(userForm.getPassword());
        utente.setNome(userForm.getNome());
        utente.setCognome(userForm.getCognome());
        utente.setCodFiscale(userForm.getCodFiscale());
        utente.setEmail(userForm.getEmail());
        utente.setTelefono(userForm.getTelefono());
        utente.setRuolo(userForm.getRuolo());
        utente.setStato(userForm.getStato());
     
        return utente;
    }

}


class UtenteBeanToUserForm{
    
    UserActionForm convert (Utente utente){
        
        UserActionForm userForm = new UserActionForm();
        
        
        userForm.setUsername(utente.getUsername());
        userForm.setNome(utente.getNome());
        userForm.setCognome(utente.getCognome());
        userForm.setPassword(utente.getPassword());
        userForm.setEmail(utente.getEmail());
        userForm.setRuolo(utente.getRuolo());
        userForm.setCodFiscale(utente.getCodFiscale());
        userForm.setTelefono(utente.getTelefono());
        userForm.setStato(utente.getStato());
       
     
        return userForm;
    }

}